package com.dao;

import com.bean.Book;
import com.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author hecn
 * @version 1.0
 * @date 2020/8/1 0001 17:22
 * @company
 */
public class BookDao {
    public void addBook(Book book) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        String sql = "insert into book.book(book_num, book_name, book_writer," +
                " book_price, publish_time, publish_house) VALUES(?,?,?,?,?,?)";

        try {
            connection = JdbcUtils.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, book.getBook_num() + 1);
            preparedStatement.setString(2, book.getBook_name());
            preparedStatement.setString(3, book.getBook_write());
            preparedStatement.setInt(4, book.getBook_price());
            preparedStatement.setString(5, book.getPublish_time());
            preparedStatement.setString(6, book.getPublish_house());
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(connection, preparedStatement);
        }
    }


    /***
     * 借还书是否存在
     * @param book_name
     * @return
     */
    public Book retandborBook(String book_name) {
        Book book = null;
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        String sql = "select * from book.book where book_name=?";
        ResultSet resultSet = null;

        try {
            connection = JdbcUtils.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, book_name);
            resultSet = preparedStatement.executeQuery();

            if (resultSet.next()) {
                book = new Book();
                book.setBook_num(resultSet.getInt(1));
                book.setBook_name(resultSet.getString(2));
                book.setBook_write(resultSet.getString("book_writer"));
                book.setBook_price(resultSet.getInt("book_price"));
                book.setPublish_time(resultSet.getString("publish_time"));
                book.setPublish_house(resultSet.getString("publish_house"));
            }


        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(connection, preparedStatement, resultSet);
        }

        return book;
    }


    public int updateBook(Book book, int i) {
        int ii=0;
        int sum = (book.getBook_num() + i);
        if (sum < 0) {
            ii=-1;
        }
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        String sql = "update book.book set book_num=? where book_name=?";

        try {
            connection = JdbcUtils.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, sum);
            preparedStatement.setString(2, book.getBook_name());
            preparedStatement.executeUpdate();


        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(connection, preparedStatement);
        }
        return ii;
    }


}
